package com.pro.common.pay.third.service;

import cn.hutool.json.JSONUtil;
import com.pro.common.api.R;
import com.pro.common.api.enums.EnumPaymentResponseType;
import com.pro.common.api.model.request.pay.PaymentNotifyData;
import com.pro.common.api.model.request.pay.PaymentResponse;
import com.pro.common.api.model.request.pay.PaymentUserDetail;
import com.pro.common.api.pay.intf.IPaymentService;
import com.pro.common.api.pay.model.db.PayMerchant;
import com.pro.common.api.pay.model.dto.PaymentParamDTO;
import com.pro.common.pay.third.enums.EnumPayMerchantPlatform;
import com.pro.framework.api.util.DateUtils;
import lombok.Getter;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@Getter
public class TestPayService implements IPaymentService {
    private final String merchantPlatform = EnumPayMerchantPlatform.test.name();

    @Override
    public PaymentResponse payment(PaymentParamDTO dto, PaymentUserDetail userDetail, PayMerchant payMerchant) {
        String notifyUrl = dto.getNotifyUrl();
        BigDecimal orderAmount = dto.getOrderAmount();
        String no = dto.getNo();
        // http post orderAmount no notifyUrl
        return PaymentResponse.builder()
                .payMethod(dto.getPayMethod())
                .no(dto.getNo())
                .orderAmount(dto.getOrderAmount())
                .type(EnumPaymentResponseType.URL)
                .value("https://alipay.com/1299031289012389")
                .expireTime(DateUtils.toTimestampMillis(LocalDateTime.now().plusMinutes(10)))
                .build();
    }

    @Override
    public PaymentNotifyData paymentNotifyRead(String params, PayMerchant payMerchant) {
        PaymentNotifyData data = JSONUtil.toBean(params, PaymentNotifyData.class);
        if (data.getStatusSuccess() == null) {
            data.setStatusSuccess(true);
        }
        return data;
    }

    @Override
    public R<?> paymentNotifyReturn(Boolean endSuccess) {
        return endSuccess ? R.ok() : R.fail(500,"test");
    }
}
